package cool.doudou.doudada.iot.codec.core.util;

import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.io.FileUtils;
import org.springframework.util.StringUtils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.jar.Attributes;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;

/**
 * FileUtil
 *
 * @author jiangcs
 * @since 2022/09/30
 */
public class FileUtil {
    /**
     * 获取 父路径
     *
     * @param manufactureId 厂商ID
     * @param productId     产品ID
     * @return 父路径
     */
    public static String getParentPath(String manufactureId, String productId) {
        String parentPath = "";
        if (StringUtils.hasLength(manufactureId)) {
            parentPath = manufactureId;
        }
        if (StringUtils.hasLength(productId)) {
            if (!"".equals(parentPath)) {
                parentPath += File.separator;
            }
            parentPath += productId;
        }
        return parentPath;
    }

    /**
     * 解压缩 zip文件
     *
     * @param zipPath     zip文件路径
     * @param zipFileName zip文件名
     * @param bundlePath  Bundle文件路径
     * @param parentPath  父路径
     * @return 文件名称集合
     */
    public static List<String> unzip(String zipPath, String zipFileName, String bundlePath, String parentPath) {
        // zip文件
        File zipFile = new File(zipPath, zipFileName);
        if (!zipFile.exists()) {
            return null;
        }
        // bundle文件 父路径
        File bundleParentFile = new File(bundlePath, parentPath);
        if (!bundleParentFile.exists()) {
            bundleParentFile.mkdirs();
        }
        // 解压缩
        return ZipUtil.unzip(zipFile, bundleParentFile.getAbsolutePath());
    }

    /**
     * 获取 Package文件信息
     *
     * @param packagePath     package文件路径
     * @param parentPath      父路径
     * @param packageFileName package文件名
     * @return package信息，JSON格式
     * @throws RuntimeException 异常
     */
    public static JSONObject getPackage(String packagePath, String parentPath, String packageFileName) throws RuntimeException {
        // package文件 父路径
        File packageParentFile = new File(packagePath, parentPath);
        if (!packageParentFile.exists()) {
            packageParentFile.mkdirs();
        }

        File packageFile = new File(packageParentFile, packageFileName);
        if (!packageFile.exists()) {
            throw new RuntimeException("packageFile not exists");
        }

        try {
            String packageStr = FileUtils.readFileToString(packageFile, StandardCharsets.UTF_8);
            return JSONObject.parseObject(packageStr, JSONObject.class);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取 Bundle文件的 SymbolicName
     *
     * @param jarPath     jar文件路径
     * @param parentPath  父路径
     * @param jarFileName jar文件名
     * @return symbolicName
     * @throws RuntimeException 异常
     */
    public static String getSymbolicName(String jarPath, String parentPath, String jarFileName) throws RuntimeException {
        // jar文件 父路径
        File jarParentFile = new File(jarPath, parentPath);
        if (!jarParentFile.exists()) {
            jarParentFile.mkdirs();
        }

        File jarFile = new File(jarParentFile, jarFileName);
        if (!jarFile.exists()) {
            throw new RuntimeException("jarFile not exists");
        }

        Manifest manifest;
        try {
            BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(jarFile));
            JarInputStream jarInputStream = new JarInputStream(bufferedInputStream);
            manifest = jarInputStream.getManifest();
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        if (manifest == null) {
            throw new RuntimeException("manifest.mf not found");
        }
        Attributes attributes = manifest.getMainAttributes();
        String bundleSymbolicName = attributes.getValue("Bundle-SymbolicName");
        if (bundleSymbolicName == null) {
            throw new RuntimeException("Bundle-SymbolicName not set");
        }
        return bundleSymbolicName;
    }

    /**
     * 读取 文件
     *
     * @param path     文件路径
     * @param fileName 文件名
     * @return 文件内容 字符串
     * @throws IOException 异常
     */
    public static String read(String path, String fileName) throws IOException {
        File file = new File(path, fileName);
        if (!file.exists()) {
            return "";
        }
        return FileUtils.readFileToString(file, StandardCharsets.UTF_8);
    }

    /**
     * 写入 文件
     *
     * @param path     文件路径
     * @param fileName 文件名
     * @param content  内容
     * @throws IOException 异常
     */
    public static void write(String path, String fileName, String content) throws IOException {
        File file = new File(path, fileName);
        FileUtils.writeStringToFile(file, content, StandardCharsets.UTF_8);
    }
}
